package feeinterface.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ulwx.database.DataBaseSet;
import com.ulwx.database.DbException;
import com.ulwx.database.RowMapper;
import com.ulwx.tool.DbUtils;
import com.ulwx.tool.StringUtils;

public class ChannelUserUseDao {
	private static final Logger log = Logger.getLogger(ChannelUserUseDao.class);

	/**
	 * 根据IMSI 或 MobileID 取得使用次数超过4，肯上行率小于30%的通道，作为不再使用的通道
	 * 
	 * @param imsi
	 *            IMSI卡号
	 * @param OverUseCnt
	 *            超过使用次数
	 * @param moRate
	 *            例如 30 表示30%
	 * @return
	 * @throws Exception
	 */
	public static String getUnUsedChannels(String imsi, int OverUseCnt,
			int moRate) throws Exception {
		String channels = "";
		String sql = "SELECT ChannelID FROM ChannelUserUse "
				+ "WHERE Imsi = ? AND UseCnt > ? AND (MoCnt * 100 / UseCnt) <= ?";
		Map<Integer, Object> args = new HashMap<Integer, Object>();
		args.put(1, imsi);
		args.put(2, OverUseCnt);
		args.put(3, moRate);

		RowMapper<String> rowMapper = new RowMapper<String>() {
			public String mapRow(DataBaseSet rs) throws Exception {
				return rs.getString("ChannelID");
			}
		};

		List<String> list = DbUtils.doQueryObject(null, sql, args, rowMapper);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				channels = channels + list.get(i) + ",";
			}
		}
		channels = StringUtils.trimTailString(channels, ",");
		return channels;

	}

	/**
	 * 根据MobileID 或 IMSI 取得不推荐使用的通道 2011-1-7
	 * 
	 * @param imsi
	 *            IMSI卡号
	 * @param SubUseCnt
	 * @return
	 * @throws DbException
	 */
	public static String getNotCommendChannels(String imsi, Integer SubUseCnt)
			throws Exception {
		String channels = "";
		String sql = "SELECT ChannelID FROM ChannelUserUse WHERE imsi = ?"
				+ " AND UseCnt - MoCnt > ?";
		Map<Integer, Object> args = new HashMap<Integer, Object>();
		args.put(1, imsi);
		args.put(2, SubUseCnt);
		RowMapper<String> rowMapper = new RowMapper<String>() {
			public String mapRow(DataBaseSet rs) throws Exception {
				return rs.getString("ChannelID");
			}
		};

		List<String> list = DbUtils.doQueryObject(null, sql, args, rowMapper);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				channels = channels + list.get(i) + ",";
			}
		}
		channels = StringUtils.trimTailString(channels, ",");
		return channels;

	}

	/**
	 * 更新ChannelUserUse表
	 * 
	 * @param imsi
	 * @param channelID
	 * @param sendTimes
	 * @throws Exception
	 */
	public static void updateUserUseByImsi(String imsi, int channelID,
			int useCnt, Date curTime) throws Exception {
		if (imsi.length() > 3 && imsi.substring(0, 3).equals("460")) {
			//考虑到部分 SP不回传上行的，所以这部分通道无需更新使用次数
		
			String sql = "insert into ChannelUserUse(Imsi,ChannelID,UseCnt,UpdateTime)values(?,?,?,?) "
					+ " ON DUPLICATE KEY  UPDATE  UseCnt = UseCnt + ?";
			Map<Integer, Object> args = new HashMap<Integer, Object>();
			args.put(1, imsi);
			args.put(2, channelID);
			args.put(3, useCnt);
			args.put(4, curTime);
			args.put(5, useCnt);
			try {
				DbUtils.executeBindUpdate(null, sql, args);
			} catch (Exception e) {
				log.error("", e);
			}
		}
	}

	public static void main(String[] args) throws Exception {

	}

}
